package ext.tianma.part.util.changeState;

import ext.generic.erp.common.CommonPDMUtil;
import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.part.forms.TMTypeUtil;
import wt.doc.WTDocument;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.fc.collections.WTHashSet;
import wt.fc.collections.WTSet;
import wt.iba.value.ReferenceValue;
import wt.lifecycle.LifeCycleHelper;
import wt.lifecycle.State;
import wt.part.WTPart;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;

/**
 * 用于存储物料的原始状态,修改物料当前状态
 * 
 * @author laichao_sun
 *
 */
public class UpdateSates {
	/**
	 * 获取状态
	 * 
	 * @param obj
	 * @return
	 */
	public static String getState(WTObject obj) {
		try {
			if (obj instanceof WTPart) {
				WTPart part = (WTPart) obj;
				String state = CommonPDMUtil.getLifecycle(part);
				return state;
			} else if (obj instanceof WTDocument) {
				WTDocument doc = (WTDocument) obj;
				String state = CommonPDMUtil.getLifecycle(doc);
				return state;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 修改状态
	 * 
	 * @param obj
	 * @param state
	 */
	public static void setState(WTObject obj, String stateStr) {
		try {
			if (StringUtil.isNullOrEmpty(stateStr)) {
				return;
			}
			State state = State.toState(stateStr);
			if (obj instanceof WTPart) {
				WTPart part = (WTPart) obj;
				LifeCycleHelper.service.setLifeCycleState(part, state);
			} else if (obj instanceof WTDocument) {
				WTDocument doc = (WTDocument) obj;
				LifeCycleHelper.service.setLifeCycleState(doc, state);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 物料修改流程中判断成品,给予正在工作的状态
	 * @param obj
	 * @return
	 */
	public static boolean isBoolean(WTObject obj) {
		if (obj instanceof WTPart) {
			WTPart part = (WTPart) obj;
			if (TMTypeUtil.isFinishedGood(part)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 获取版本号
	 * @param obj
	 * @return
	 */
	public static String getPartVersion(WTObject obj) {
		try {
			if (obj instanceof WTPart) {
				WTPart part = (WTPart) obj;
				String partVersion = CommonPDMUtil.getVersion(part);
				return partVersion;
			} else if (obj instanceof WTDocument) {
				WTDocument doc = (WTDocument) obj;
				String partVersion = CommonPDMUtil.getVersion(doc);
				return partVersion;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 检验版本是否有变化
	 * @param obj
	 * @param string
	 * @throws WTException
	 */
	public static void checkPartVersion(WTObject obj,String string) throws WTException {
		if (StringUtil.isNullOrEmpty(string)) {
			return;
		}
		if (obj instanceof WTPart) {
			WTPart part = (WTPart) obj;
			String partVersion = CommonPDMUtil.getVersion(part);
			if(string.equals(partVersion)) {
				throw new WTException("物料信息未修改,无法完成任务!");
			}
		}
	}
	
	
	/**
	 * 完成任务,校验分类属性
	 * @param obj
	 * @throws WTException
	 */
	public static void checkPartClassification(WTObject obj) throws WTException {
		if (obj instanceof WTPart) {
			WTPart part = (WTPart) obj;
			String classification = ClassificationUtil.getClassification(part);
			if (StringUtil.isNullOrEmpty(classification)) {
				throw new WTException("请维护该物料的分类!");
			}else {
				WTSet refValueSet = new WTHashSet();
				QuerySpec qs = new QuerySpec(ReferenceValue.class);
				qs.appendWhere(new SearchCondition(ReferenceValue.class, ReferenceValue.IBAHOLDER_REFERENCE + ".key", "=",
						PersistenceHelper.getObjectIdentifier(part)), new int[] { 0 });
				QueryResult qr = PersistenceHelper.manager.find(qs);
				while (qr.hasMoreElements()) {
					refValueSet.add(qr.nextElement());
				}
				if(!(refValueSet.size() == 1)) {
					throw new WTException("请保证该物料只有一个分类!");
				}
				if(!classification.contains("MDM")) {
					throw new WTException("请选择数据标准化下的分类!");
				}
			}
		}
	}
}
